package com.yiwei.zxp.activity;

import android.os.Bundle;
import android.support.v4.widget.SwipeRefreshLayout;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.yiwei.zxp.R;
import com.yiwei.zxp.adapter.MallGoodsAdapter;
import com.yiwei.zxp.api.Api;
import com.yiwei.zxp.api.bean.ShopMallListBean;
import com.yiwei.zxp.base.BaseActivity;
import com.yiwei.zxp.constant.UserInfoGlobal;
import com.yiwei.zxp.util.Network;
import com.yiwei.zxp.util.RxLifeUtil;
import com.yiwei.zxp.util.ToastUtil;
import com.yiwei.zxp.util.UI;
import com.yiwei.zxp.util.Util;
import com.yiwei.zxp.view.OnLoadeMoreListener;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;
import butterknife.Unbinder;

/**
 * Created by xsooy-pc on 17-12-8.
 */

public class SearchResultActivity extends BaseActivity {


    @BindView(R.id.btn_menu)
    ImageView btnMenu;
    @BindView(R.id.btn_search)
    TextView btnSearch;
    @BindView(R.id.btn_message)
    ImageView btnMessage;
    @BindView(R.id.txt_message_num)
    TextView txtMessageNum;
    @BindView(R.id.btn_tab_one)
    TextView btnTabOne;
    @BindView(R.id.btn_tab_two)
    TextView btnTabTwo;
    @BindView(R.id.btn_tab_three)
    TextView btnTabThree;
    @BindView(R.id.list)
    RecyclerView list;
    @BindView(R.id.refresh)
    SwipeRefreshLayout refresh;
    @BindView(R.id.btn_delete)
    TextView btnDelete;
    @BindView(R.id.layout_search)
    LinearLayout layoutSearch;
    @BindView(R.id.img_price)
    ImageView imgPrice;
    @BindView(R.id.layout_empty)
    LinearLayout layoutEmpty;
    private Unbinder unbiner;
    private String keyword;
    private MallGoodsAdapter adapter;
    private String order_by = "1"; //条件排序（1：综合排序（销量和价格），2：销量排序，3：价格排序）不填默认为按价格排序
    private String sort; //排序（1：递增，0：递减）不填默认为倒序
    private boolean isPriceUp = false;
    private int selectPoi = 0;
    private OnLoadeMoreListener listener = new OnLoadeMoreListener() {
        @Override
        public void onLoadMore(int page) {
            getShopMallList(page);
        }
    };
    private int color_red = 0xffFF0030;
    private int color_black = 0xff000000;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_search_result);
        initBar();
        unbiner = ButterKnife.bind(this);
        init();
        getShopMallList(1);
    }

    private void init() {
        adapter = new MallGoodsAdapter(this);
        GridLayoutManager layoutManager = new GridLayoutManager(this, 2);
        list.setLayoutManager(layoutManager);
        list.setAdapter(adapter);
        refresh.setOnRefreshListener(new SwipeRefreshLayout.OnRefreshListener() {
            @Override
            public void onRefresh() {
                listener.setPage(1);
                getShopMallList(1);
                getMsgTotal();
            }
        });
        keyword = getIntent().getStringExtra("keyword");
        btnSearch.setText(keyword);
        getShopMallList(1);
    }

    @Override
    protected void onResume() {
        super.onResume();
        if (UserInfoGlobal.isLogin()) {
            getMsgTotal();
        }
    }

    public void getMsgTotal() {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }
        Api.api_service.getMessageCenter().compose(RxLifeUtil.NocheckOn(this)).
                doFinally(() -> {
                    try {

                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    if (result.code.equals("00")) {
                        if (result.datas.totalCount == 0) {
                            txtMessageNum.setVisibility(View.GONE);
                        } else {
                            txtMessageNum.setVisibility(View.VISIBLE);
                            txtMessageNum.setText(result.datas.totalCount + "");
                        }
                    }

                });
    }

    public void getShopMallList(int page) {
        if (!Network.checkNetwork(this)) {
            ToastUtil.show(this, "网络连接失败");
            return;
        }

        refresh.setRefreshing(true);
        ShopMallListBean bean = new ShopMallListBean();
        bean.page = page;
        bean.keyword = keyword;
        bean.order_by = order_by;
        if (order_by.equals("3")) {
            bean.sort = sort;
        }

        Api.api_service.getShopMallList(bean).compose(RxLifeUtil.checkOn(this)).
                doFinally(() -> {
                    try {
                        refresh.setRefreshing(false);
                    } catch (NullPointerException e) {

                    }
                }).
                subscribe(result -> {
                    adapter.add(result.datas, page == 1);
                    if (adapter.getItemCount() == 0) {
                        layoutEmpty.setVisibility(View.VISIBLE);
                    } else {
                        layoutEmpty.setVisibility(View.GONE);
                    }
                });
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        unbiner.unbind();
        unbiner = null;
    }


    @OnClick({R.id.layout_reback,R.id.btn_message, R.id.btn_menu, R.id.btn_delete})
    public void onViewClicked(View view) {
        if (UI.isFastClick()) {
            return;
        }
        switch (view.getId()) {
            case R.id.btn_message:
                if (!UserInfoGlobal.isLogin()) {
                    Util.startActivity(this, LoginNewActivity.class);
                    return;
                }
                Util.startActivity(this, BussinessMsgTotalActivity.class);
                break;
            case R.id.layout_reback:
               finish();
                break;
            case R.id.btn_menu:
                finish();
                break;
            case R.id.btn_delete:
                finish();
//                layoutSearch.setVisibility(View.GONE);
//                keyword = "";
                break;
        }
    }

    private void setStatus(int poi) {
        selectPoi = poi;
        switch (poi) {
            case 0:
                btnTabOne.setTextColor(color_red);
                btnTabTwo.setTextColor(color_black);
                btnTabThree.setTextColor(color_black);
                imgPrice.setImageResource(R.mipmap.img_price_default);
                break;
            case 1:
                btnTabOne.setTextColor(color_black);
                btnTabTwo.setTextColor(color_red);
                btnTabThree.setTextColor(color_black);
                imgPrice.setImageResource(R.mipmap.img_price_default);
                break;
            case 2:
                btnTabOne.setTextColor(color_black);
                btnTabTwo.setTextColor(color_black);
                btnTabThree.setTextColor(color_red);
                break;
        }
    }

    @OnClick({ R.id.btn_tab_one, R.id.btn_tab_two, R.id.btn_tab_three})
    public void onViewClicked1(View view) {
        if (UI.isFastClick()) {
            return;
        }
        listener.setPage(1);
        switch (view.getId()) {
            case R.id.btn_tab_one:
                if (selectPoi == 0) {
                    return;
                }
                setStatus(0);
                order_by = "1";
                btnTabOne.setTextColor(color_red);
                isPriceUp = false;
                getShopMallList(1);
                break;
            case R.id.btn_tab_two:
                if (selectPoi == 1) {
                    return;
                }
                setStatus(1);
                order_by = "2";
                isPriceUp = false;
                getShopMallList(1);
                break;
            case R.id.btn_tab_three:
                setStatus(2);
                isPriceUp = !isPriceUp;
                if (isPriceUp) {
                    imgPrice.setImageResource(R.mipmap.img_price_up);
                    sort = "1";
                } else {
                    imgPrice.setImageResource(R.mipmap.img_price_down);
                    sort = "0";
                }
                order_by = "3";
                getShopMallList(1);
                break;
        }
    }
}
